/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.smartlink;

import org.smartlink.provider.SmartLink;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * An activity that will edit the title of a note. Displays a floating window
 * with a text field.
 */
public class KeyEditor extends Activity implements View.OnClickListener {
	private static final String TAG = "KeyEditor";

	/**
	 * This is a special intent action that means "edit the title of a note".
	 */
	public static final String EDIT_KEY_ACTION = "org.smartlink.action.EDIT_KEY";

	/**
	 * Index of the title column
	 */
	private static final int KEY_INDEX = 1;

	/**
	 * An array of the columns we are interested in.
	 */
	private static final String[] PROJECTION = new String[] { SmartLink.Links._ID, // 0
			SmartLink.Links.KEY, // 1
	};

	/**
	 * Cursor which will provide access to the note whose title we are editing.
	 */
	Cursor mCursor;

	/**
	 * The EditText field from our UI. Keep track of this so we can extract the
	 * text when we are finished.
	 */
	EditText mText;

	/** The different distinct states the activity can be run in. */
	private int mState;
	/** editing a already existing link */
	private static final int STATE_EDIT = 0;
	/** editing a to-be-inserted new link */
	private static final int STATE_INSERT = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setContentView(R.layout.key_editor);

		// Get the uri of the note whose title we want to edit
		Uri uri = getIntent().getData();

		String action = getIntent().getAction();
		if (action.equals(Intent.INSERT_ACTION)) {
			mState = STATE_INSERT;
			Log.w(TAG, "insert mode");
			String key = getIntent().getStringExtra(SmartLink.Links.EXTRA_CREATE_LINK);
			ContentValues initialValues = new ContentValues();
			initialValues.put(SmartLink.Links.KEY, key);
			uri = getContentResolver().insert(getIntent().getData(), initialValues);
			// If we were unable to create a new link, then just finish
			// this activity. A RESULT_CANCELED will be sent back to the
			// original activity if they requested a result.
			if (uri == null) {
				Log.e(TAG, "Failed to insert new link into " + getIntent().getData());
				finish();
				return;
			}

			// The new entry was created, so assume all will end well and
			// set the result to be returned.
			setResult(RESULT_OK, uri.toString());

		} else if (action.equals(Intent.EDIT_ACTION)) {
			mState = STATE_EDIT;
		}
		// Get a cursor to access the note
		mCursor = managedQuery(uri, PROJECTION, null, null);

		// Set up click handlers for the text field and button
		mText = (EditText) this.findViewById(R.id.key);
		mText.setOnClickListener(this);
		mText.setMaxLines(1);
		mText.setSingleLine();

		Button b = (Button) findViewById(R.id.ok);
		b.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Initialize the text with the title column from the cursor
		if (mCursor != null) {
			mCursor.first();
			if (mState == STATE_EDIT) {
				setTitle(getText(R.string.title_edit));
			} else {
				setTitle(getText(R.string.title_edit_key));
			}
			String title = mCursor.getString(KEY_INDEX);
			mText.setText(title);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

		// Write the text back into the cursor
		if (mCursor != null) {
			String key = mText.getText().toString();
			mCursor.updateString(KEY_INDEX, key);
			mCursor.commitUpdates();
		}
	}

	public void onClick(View v) {
		if (mState == STATE_INSERT) {
			// if we are inserting, we should immediately ask the user to pick a
			// target

		}
		finish();
	}
}
